package com.lc.advertising.ui.activity;

import android.content.Intent;
import android.os.Bundle;

import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.other.IntentKey;
import com.lc.advertising.util.CourseUtils;
import com.lc.advertising.util.SPUtil;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.NonNull;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 闪屏界面
 */
public final class SplashActivity extends AppActivity {




    @Override
    protected int getLayoutId() {
        return R.layout.splash_activity;
    }

    @Override
    protected void initView() {



    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if ("".equals(SPUtil.get(SplashActivity.this,"agree",""))||SPUtil.get(SplashActivity.this,"agree","").isEmpty()||SPUtil.get(SplashActivity.this,"agree","")==null){

                            Intent intent = new Intent(getContext(), GuideActivity.class);
                            getContext().startActivity(intent);
                            finish();
                        }else {

                            if (CourseUtils.isNullOrEmpty(SPUtil.get(SplashActivity.this, IntentKey.UID,""))){
                                Intent intent = new Intent(getContext(), LoginActivity.class);
                                getContext().startActivity(intent);
                            }else {
                                Intent intent = new Intent(getContext(), MainActivity.class);
                                getContext().startActivity(intent);
                            }

                            finish();
                        }

                    }
                });
            }
        };
        timer.schedule(task,3000);
    }

    @Override
    protected void initData() {
    }

    @NonNull
    @Override
    protected ImmersionBar createStatusBarConfig() {
        return super.createStatusBarConfig()
                // 隐藏状态栏和导航栏
                .hideBar(BarHide.FLAG_HIDE_BAR);
    }

    @Override
    public void onBackPressed() {
        //禁用返回键
        //super.onBackPressed();
    }

    @Override
    protected void initActivity() {
        // 问题及方案：https://www.cnblogs.com/net168/p/5722752.html
        // 如果当前 Activity 不是任务栈中的第一个 Activity
        if (!isTaskRoot()) {
            Intent intent = getIntent();
            // 如果当前 Activity 是通过桌面图标启动进入的
            if (intent != null && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
                    && Intent.ACTION_MAIN.equals(intent.getAction())) {
                // 对当前 Activity 执行销毁操作，避免重复实例化入口
                finish();
                return;
            }
        }
        super.initActivity();
    }

    @Deprecated
    @Override
    protected void onDestroy() {
        // 因为修复了一个启动页被重复启动的问题，所以有可能 Activity 还没有初始化完成就已经销毁了
        // 所以如果需要在此处释放对象资源需要先对这个对象进行判空，否则可能会导致空指针异常
        super.onDestroy();
    }
}